﻿using MiniExcelLibs;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.工厂.车间
{
    public partial class Form产线产废查询 : UIPage
    {
        public int 开始行 { get; private set; }
        public int 结束行 { get; private set; }
        public Form产线产废查询()
        {
            InitializeComponent();
        }

        private void Form产线产废查询_Load(object sender, EventArgs e)
        {
            this.uiDatePicker开始日期.Value = DateTime.Today;
            this.uiDatePicker结束日期.Value = DateTime.Today;
            ListWorkLine("", this.uiComboBox产线);
            开始行 = -1;
            结束行 = -1;
        }
        private void ListWorkLine(string WorkType, UIComboBox uiComboBox产线)
        {
            string sql;
            sql = @"SELECT '' WorkLineCode,'全部' WorkLineName
                    union all
                    SELECT   WorkLineCode, WorkLineName
                    FROM      Dic_WorkLine where 1=1 ";
            if (WorkType != "")
            {
                sql += " and WorkType='" + WorkType + "'";

            }
            sql += "  order by WorkLineCode ";

            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            uiComboBox产线.DataSource = tb;
            uiComboBox产线.DisplayMember = "WorkLineName";
            uiComboBox产线.ValueMember = "WorkLineName";
            uiComboBox产线.Refresh();


        }
        private void uiButton刷新_Click(object sender, EventArgs e)
        {
            List辅料产废();
        }

        private void List辅料产废()
        {
            string sql;
            sql = @" SELECT  row_number() over(order by CreateDate,WasteGuid) 序号,  WasteGuid, WasteType,  WasteDate,WorkShopCode,  InventoryCode, InventoryName, Size, Batch, QuantityMain,Unit, 
            CreateID, Creator, CreateDate,  WorkLineName,TeamName,Status  " +
                " FROM      Bus_Waste  where 1=1 and WasteDate>='" + this.uiDatePicker开始日期.Value.ToString("yyyy-MM-dd") + "' and WasteDate<'" + this.uiDatePicker结束日期.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
            if (this.uiComboBox产线.SelectedText != "全部")
            {
                sql += " and WorkLineName='" + this.uiComboBox产线.SelectedText + "'";
            }
            if (this.uiComboBoxWorkTeam.SelectedText != "全部")
            {
                sql += " and TeamName='" + this.uiComboBoxWorkTeam.SelectedText + "'";
            }
            if (this.uiTextBox存货编号.Text != "")
            {
                sql += " and InventoryCode='" + this.uiTextBox存货编号.Text.Replace("--", "") + "'";
            }
            if (this.uiRadioButton酸洗.Checked)
            {
                sql += " and WasteType='酸连轧'";
            }
            if (this.uiRadioButton镀锌.Checked)
            {
                sql += " and WasteType='镀锌'";
            }
            if (this.uiRadioButton彩涂.Checked)
            {
                sql += " and WasteType='彩涂'";
            }
           
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                this.uiDataGridView临时.AutoGenerateColumns = false;
                this.uiDataGridView临时.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                this.uiDataGridView临时.DataSource = tb;
                this.uiDataGridView临时.Refresh();
            }
            else
            {
                this.uiDataGridView临时.AutoGenerateColumns = false;
                this.uiDataGridView临时.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                this.uiDataGridView临时.DataSource = null;
                this.uiDataGridView临时.Refresh();
            }
        }

        private void uiComboBox产线_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql;
            string WorkLineName = this.uiComboBox产线.SelectedText;
            sql = " select '全部' as WorkTeamCode ";
            sql += " union all ";

            sql += "  select  distinct  WorkTeamCode  from  [Dic_WorkLine]   join Dic_TeamLine on ";
            sql += "[Dic_WorkLine].WorkLineCode = dbo.Dic_TeamLine.WorkLineCode  where 1=1  ";
            if (WorkLineName != "全部")
            {
                sql += " and  Dic_WorkLine.WorkLineName ='" + WorkLineName + "'";
            }
            // sql += " order by WorkTeamCode ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                this.uiComboBoxWorkTeam.DataSource = tb;
                this.uiComboBoxWorkTeam.DisplayMember = "WorkTeamCode";
                this.uiComboBoxWorkTeam.ValueMember = "WorkTeamCode";
                this.uiComboBoxWorkTeam.Refresh();
            }
            else
            {
                this.uiComboBoxWorkTeam.DataSource = null;
            }
        }

        private void uiButton导出_Click(object sender, EventArgs e)
        {
            string path;
            string sql;
            sql = @" SELECT  row_number() over(order by CreateDate,WasteGuid) 序号,    WasteType 加工类型, WorkLineName 产线名称,TeamName 班组名称, WasteDate 产废日期,   InventoryCode 存货编号, InventoryName 存货名称, Size 规格型号, Batch 批号, QuantityMain 数量,Unit 单位, 
                    Creator 操作人, CreateDate 操作时间,  Status 状态 " +
                " FROM      Bus_Waste  where 1=1 and WasteDate>='" + this.uiDatePicker开始日期.Value + "' and WasteDate<'" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";
            if (this.uiComboBox产线.SelectedText != "全部")
            {
                sql += " and WorkLineName='" + this.uiComboBox产线.SelectedText + "'";
            }
            if (this.uiComboBoxWorkTeam.SelectedText != "全部")
            {
                sql += " and TeamName='" + this.uiComboBoxWorkTeam.SelectedText + "'";
            }
            if (this.uiTextBox存货编号.Text != "")
            {
                sql += " and InventoryCode='" + this.uiTextBox存货编号.Text.Replace("--", "") + "'";
            }
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);

            saveFileDialog1.Filter = "Excel文件 (*.XLSX)|*.XLSX";
            if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                path = this.saveFileDialog1.FileName;
                if (!File.Exists(path))
                {
                    MiniExcel.SaveAs(path, tb, true, "sheet1", ExcelType.XLSX);
                }
                else
                {

                    MiniExcel.SaveAs(path, tb, true, "sheet1", ExcelType.XLSX, null, true);
                    // 
                    return;
                }

            }
        }
    }
}
